<?php
/**
 * XIE--Scorpion
 *
 * 前台登陆展示
 *
 * time 2016-5-13
 */
namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB,Input,Redirect,Session,url;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Home\User;

class LoginController extends Controller
{
    /**
	 * 登陆页面
	 */
    public function login()
    {
		//判断是否传值
		if($_GET)
		{
		   //接值
           $user_email=isset($_GET['email'])?$_GET['email']:'';
           $user_pwd=md5(isset($_GET['password'])?$_GET['password']:'');
		   //检测用户名否正确
		   $user_email=DB::table('user')->where('user_email',$user_email)->get();
		   //print_r($user_email);die;
		   if($user_email)
		   {    
			   //检测密码是否正确
			   $user_pwd=DB::table('user')->where('user_pwd',$user_pwd)->get();
			   if($user_pwd)
			   {
					//取出用户名和对应的ID
					foreach($user_email as $v)
					{
					   $user_name=$v->user_name;
					   $user_id=$v->user_id;
					}
					//将用户名和对应的ID存入Session
					Session::put('user_name',$user_name);
					Session::put('user_id',$user_id);
					echo 1;
			   }else
			   {
				  echo 2;
			   }
			   
			}else
			{
				 echo 3;
			}
		}else
		{
		   return view('homes/login/login');
		}
    }
	/**
	 * 注册页面
	 */
    public function register()
    {
		//判断是否传值
		if($_GET)
		{
			//接值
			//print_r($_GET);die;
			$user_type=isset($_GET['type'])?$_GET['type']:'';			
			if($user_type==1)
			{
				$data['admin_pwd']=md5(isset($_GET['password'])?$_GET['password']:'');
			    $data['admin_email']=isset($_GET['email'])?$_GET['email']:'';
			    $data['admin_user']=isset($_GET['user_name'])?$_GET['user_name']:'';
                $data['admin_phone']=isset($_GET['user_phone'])?$_GET['user_phone']:'';
                //注册信息入库并且取出相应的ID
				$user_id=DB::table('admin_user')->insertGetid($data);	
				if($user_id)
				{
				   echo "<script>alert('注册成功');</script>";
				   return Redirect::action('admin\IndexController@login');
				}else
				{
				   echo "<script>alert('注册失败');</script>";
				   return view('homes/login/login');
				}   
			}else
			{ 
				$data['user_pwd']=md5(isset($_GET['password'])?$_GET['password']:'');
			    $data['user_email']=isset($_GET['email'])?$_GET['email']:'';
			    $data['user_name']=isset($_GET['user_name'])?$_GET['user_name']:'';
                $data['user_phone']=isset($_GET['user_phone'])?$_GET['user_phone']:'';
                //注册信息入库并且取出相应的ID
				$data['user_type']=$user_type;
				$user_id=DB::table('user')->insertGetid($data);	
				if($user_id)
				{
				   //将用户名和对应的ID存入Session
				   Session::put('user_name',$data['user_name']);
				   Session::put('user_id',$user_id);
				   echo "<script>alert('注册成功');</script>";
				   return redirect('index');
				}else
				{
				   echo "<script>alert('注册失败');</script>";
				   return view('homes/login/login');
				}   
			}			
			
		}else
		{
            return view('homes/login/register');
		}        
    }
	/**
	 * 检测唯一性
	 */
    public function check_only()
    {
	   $action=isset($_REQUEST['act'])?$_REQUEST['act']:'defined';
	   if($action=='check_email')
		{
           $user_email=$_GET['email'];
		   //print_r($user_email);die;
		   $re=DB::table('user')->where('user_email',$user_email)->get();
		   if($re)
			{
                echo '1';

			}else
			{
                echo '0';

			}
		}else if($action=='check_name')
		{
           $user_name=$_GET['user_name'];
		   //print_r($user_email);die;
		   $re=DB::table('user')->where('user_name',$user_name)->get();
		   if($re)
			{
                echo '1';

			}else
			{
                echo '0';

			}
		}
	}
	/**
	 * 退出
	 */
    public function login_out()
    {
	   //清除Session
	   Session::flush();
	   return redirect('index');
	}
}
?>